package br.com.edumobi.fragments;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import br.com.edumobi.R;

public class EsqueciASenhaFragment extends GenericoFragment {

    private TextView textViewNomeTelaEsqueciASenhaFragment;
    private TextView textViewOrientacaoEsqueciASenhaFragment;
    private EditText editTextEmailEsqueciASenhaFragment;
    private Button buttonGerarNovaSenhaEsqueciASenhaFragment;
    private ProgressBar progressBarEsqueciASenhaFragment;

    private AcoesEsqueciASenhaFragment activity;
    private EsqueciASenhaTask task;

    private String email;

    View.OnClickListener clickButtonGerarNovaSenhaEsqueciASenhaFragment = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            email = editTextEmailEsqueciASenhaFragment.getText().toString();

            if (email.isEmpty()) {
                activity.falarEExibirTexto(R.string.obrigatoriedade_email);
            } else {
                if (activity.conectado()) {
                    if (task == null || task.getStatus().equals(AsyncTask.Status.FINISHED)) {
                        task = new EsqueciASenhaTask();
                        task.execute();
                    }
                } else {
                    activity.falarEExibirTexto(R.string.excecao_sem_conexao_internet);
                }
            }
        }
    };

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        activity = (AcoesEsqueciASenhaFragment) getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_esqueci_a_senha, container, false);

        textViewNomeTelaEsqueciASenhaFragment = (TextView) view.findViewById(R.id.textViewNomeTelaEsqueciASenhaFragment);
        textViewOrientacaoEsqueciASenhaFragment = (TextView) view.findViewById(R.id.textViewOrientacaoEsqueciASenhaFragment);
        editTextEmailEsqueciASenhaFragment = (EditText) view.findViewById(R.id.editTextEmailEsqueciASenhaFragment);
        buttonGerarNovaSenhaEsqueciASenhaFragment = (Button) view.findViewById(R.id.buttonGerarNovaSenhaEsqueciASenhaFragment);
        progressBarEsqueciASenhaFragment = (ProgressBar) view.findViewById(R.id.progressBarEsqueciASenhaFragment);

        buttonGerarNovaSenhaEsqueciASenhaFragment.setOnClickListener(clickButtonGerarNovaSenhaEsqueciASenhaFragment);

        if (task != null && task.getStatus().equals(AsyncTask.Status.RUNNING)) {
            esconderComponentes();
            progressBarEsqueciASenhaFragment.setVisibility(View.VISIBLE);
        }

        return view;
    }

    @Override
    public void exibirComponentes() {
        textViewNomeTelaEsqueciASenhaFragment.setVisibility(View.VISIBLE);
        textViewOrientacaoEsqueciASenhaFragment.setVisibility(View.VISIBLE);
        editTextEmailEsqueciASenhaFragment.setVisibility(View.VISIBLE);
        buttonGerarNovaSenhaEsqueciASenhaFragment.setVisibility(View.VISIBLE);
        progressBarEsqueciASenhaFragment.setVisibility(View.VISIBLE);
    }

    @Override
    public void esconderComponentes() {
        textViewNomeTelaEsqueciASenhaFragment.setVisibility(View.GONE);
        textViewOrientacaoEsqueciASenhaFragment.setVisibility(View.GONE);
        editTextEmailEsqueciASenhaFragment.setVisibility(View.GONE);
        buttonGerarNovaSenhaEsqueciASenhaFragment.setVisibility(View.GONE);
        progressBarEsqueciASenhaFragment.setVisibility(View.GONE);
    }

    private class EsqueciASenhaTask extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected void onPreExecute() {
            esconderComponentes();
            progressBarEsqueciASenhaFragment.setVisibility(View.VISIBLE);
            activity.falarTexto(R.string.aguarde);
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            return activity.regerarSenha(email);
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            exibirComponentes();
            progressBarEsqueciASenhaFragment.setVisibility(View.GONE);

            if (aBoolean) {
                activity.exibirTexto(R.string.senha_regerada_com_sucesso);
                activity.finalizar();
            } else {
                activity.falarEExibirTexto(R.string.excecao_regerar_senha_aluno_malsucedido);
            }
        }
    }

    public interface AcoesEsqueciASenhaFragment extends AcoesGenericoFragment {
        Boolean regerarSenha(String email);
    }
}